package com.content.view.Frame.MainPanel.MPOnlineResBook
{
	import com.content.model.Const.TransitionType;
	import com.content.model.GlobalVarProxy;
	import com.content.model.LoadedAssets.ConfigProxy;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.model.OnlineRes.OnlineResType;
	import com.content.view.Component.OnlineRes.OnlineResEvent;
	import com.content.view.Component.OnlineRes.SPResDetail;
	import com.content.view.Component.OnlineRes.SPShelf;
	import com.content.view.Frame.Content.Content;
	import com.content.view.Frame.MainPanel.MPBase;
	import com.content.view.Frame.MainPanel.MPBaseMediator;
	import com.content.view.Share.Player.BookPlayer_Mediator;
	import com.library.core.IDisposable;
	
	import flash.display.Sprite;
	import flash.events.DataEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public final class MPOnlineResBook_Mediator extends MPBaseMediator
	{
		public static const NAME:String							= 'MPOnlineResBook';
		
		public function MPOnlineResBook_Mediator(viewComponent:Object=null)
		{
			super(this, NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			var bgSkin:Sprite = new SkinProxy.IMG_BG_MainPanel() as Sprite;				
			_view = new MPOnlineResBook(	pConfig.UI_MainPanel_MarginTop,
				pConfig.UI_MainPanel_MarginBottom,
				pConfig.UI_MainPanel_MarginLeft,
				pConfig.UI_MainPanel_MarginRight,
				pConfig.UI_CBarL1_Width + pConfig.UI_CBarL1_MarginLeft,
				M_Book,	bgSkin);			
			ParentView.addChild( _view );
			_view.addEventListener(MPBase.ON_SHOWN, OnShown);
			_view.addEventListener(SPShelf.CHOOSE_CATEGORY, OnChooseCategory);
			_view.addEventListener(SPResDetail.DOWNLOAD, OnDownloadRes);
		}
		
		/* Event Handler
		* ----------------------------------------------------------- */
		private function OnShown(evt:DataEvent):void{
			pWebAPI.GetCategoryPublicBook(MPOnlineResBook(_view).UpdateCategory);
			pWebAPI.GetPurchasedItemsByRes(MPOnlineResBook(_view).UpdateCart,
															OnlineResType.BOOK);
		}
		
		private function OnChooseCategory(evt:DataEvent):void{
			pWebAPI.GetPublicResourcePerPage(MPOnlineResBook(_view).UpdateResource,
															1, 100, "book", evt.data, "");
		}
		
		private function OnDownloadRes(evt:OnlineResEvent):void{
			pWebAPI.PurchaseResource(null, evt.OLResVO.ResID, OnlineResType.BOOK);
		}
		
		private function get M_Book():BookPlayer_Mediator{return facade.retrieveProxy( BookPlayer_Mediator.NAME ) as BookPlayer_Mediator;}	
		
		override public function Dispose():void	{
			_view.removeEventListener(MPBase.ON_SHOWN, OnShown);
			_view.removeEventListener(SPShelf.CHOOSE_CATEGORY, OnChooseCategory);
			_view.removeEventListener(SPResDetail.DOWNLOAD, OnDownloadRes);
		}
	}
}